package com.sprouting.personal.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sprouting.personal.annotations.ApiIdempotent;
import com.sprouting.personal.model.common.ResultJson;
import com.sprouting.personal.model.common.jsr.AddGroup;
import com.sprouting.personal.model.entity.Menstruation;
import com.sprouting.personal.service.MenstruationService;
import com.sprouting.personal.utils.CheckTool;
import com.sprouting.personal.utils.EntityTool;
import com.sprouting.personal.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 月经情况
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2023/1/29 16:33
 */
@RequestMapping("/menstruation")
@RestController
@Slf4j
public class MenstruationController {

    @Autowired
    private MenstruationService menstruationService;

    /**
     * 保存月经信息
     * @param menstruation 月经信息
     * @return
     */
    @PostMapping("/saveMenstruation")
    @ApiIdempotent
    public ResultJson saveMenstruation(@Validated(AddGroup.class) Menstruation menstruation){
        if (menstruation.getMenstruationTime() == null){
            return ResultJson.error("必须选择月经时间");
        }
        Menstruation menstruationDb = menstruationService.getMenstruationByTime(StpUtil.getLoginIdAsString(), menstruation.getMenstruationTime());
        if (menstruationDb != null){
            // 已经添加
            return ResultJson.success();
        }

        menstruation.setId(IdTool.getId());
        menstruation.setUserId(StpUtil.getLoginIdAsString());
        EntityTool.buildSaveBase(menstruation, StpUtil.getLoginIdAsString());
        menstruationService.save(menstruation);
        return ResultJson.success();
    }

    /**
     * 取消月经记录
     * @param menstruationTime 月经时间，年月日
     * @return
     */
    @PostMapping("/cancelMenstruation")
    @ApiIdempotent
    public ResultJson cancelMenstruation(String menstruationTime){
        if (CheckTool.checkNull(menstruationTime)){
            return ResultJson.errorIllegal();
        }
        Menstruation menstruation = menstruationService.getMenstruationByTime(StpUtil.getLoginIdAsString(), DateUtil.parseDate(menstruationTime));
        if (menstruation != null){
            menstruationService.removeById(menstruation.getId());
        }
        return ResultJson.success();
    }

    /**
     * 查询 上月 + 本月 + 下个月的月经情况
     * @param time 要查询的时间，不传则从当前时间开始
     * @return
     */
    @PostMapping("/getMenstruation")
    public ResultJson getMenstruation(String time){
        Date date;
        if (CheckTool.checkNull(time)){
            date = new Date();
        } else {
            date = DateUtil.parseDate(time);
        }

        // 获取上个月初始日期和下个月结束日期
        DateTime before = DateUtil.offsetMonth(date, -1);
        DateTime after = DateUtil.offsetMonth(date, 1);
        DateTime beforeStart = DateUtil.beginOfMonth(before);
        DateTime afterEnd = DateUtil.endOfMonth(after);
        return ResultJson.success(menstruationService.getMenstruationSectionTime(StpUtil.getLoginIdAsString(), beforeStart, afterEnd));
    }
}
